﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Internal/Internal.Master" AutoEventWireup="true"
    CodeBehind="RoleManagement.aspx.cs" Inherits="Landscape.WebApp.Internal.RoleManagement"
    Theme="Internal" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript" src="../../Scripts/ModuleMethods/RoleManagement.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <telerik:RadToolBar ID="rtbModuleActions" runat="server" Width="100%" OnClientButtonClicked="rtbModuleActions_OnClientButtonClicked"
        EnableShadows="True">
        <Items>
           <telerik:RadToolBarButton runat="server" CommandName="LIST" ImageUrl="~/Images/Search.png"
                Text="Search" CheckOnClick="True" ImagePosition="AboveText" Value="SEARCH">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="NEW" ImageUrl="~/Images/Write.png"
                Text="New" CheckOnClick="True" ImagePosition="AboveText"  Value="NEW">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="SAVE" ImageUrl="~/Images/Floppy.png"
                Text="Save" CheckOnClick="True" ImagePosition="AboveText" Value="SAVE">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="DELETE" ImageUrl="~/Images/Delete_32x32.png"
                Text="Delete" CheckOnClick="True" ImagePosition="AboveText" Value="DELETE">
            </telerik:RadToolBarButton>
        </Items>
    </telerik:RadToolBar>
    <telerik:RadTabStrip ID="rtsRoleManagement" runat="server" MultiPageID="RadMultiPage1"
        SelectedIndex="1" Width="100%">
        <Tabs>
            <telerik:RadTab runat="server" Text="Search Results" PageViewID="RadPageView1">
            </telerik:RadTab>
            <telerik:RadTab runat="server" Text="Role Details" PageViewID="RadPageView2" 
                Selected="True">
            </telerik:RadTab>
        </Tabs>
    </telerik:RadTabStrip>
    <telerik:RadMultiPage ID="RadMultiPage1" runat="server" SelectedIndex="1" 
        Width="100%">
        <telerik:RadPageView ID="RadPageView1" runat="server">
            <div class="ContentTabBody">
                <telerik:RadGrid ID="rgRoles" runat="server" AutoGenerateColumns="False" CellSpacing="0"
                    GridLines="None" AllowMultiRowSelection="True" AllowPaging="True">
                    <ClientSettings>
                        <Selecting CellSelectionMode="None" AllowRowSelect="true" />
                        <ClientEvents OnRowDataBound="rgRoles_OnRowDataBound" OnCommand="rgRoles_OnCommand"
                            OnGridCreated="rgRoles_OnGridCreated" />
                    </ClientSettings>
                    <MasterTableView>
                        <CommandItemSettings ExportToPdfText="Export to PDF" />
                        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
                            <HeaderStyle Width="20px" />
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
                            <HeaderStyle Width="20px" />
                        </ExpandCollapseColumn>
                        <Columns>
                            <telerik:GridClientSelectColumn FilterControlAltText="Filter column1 column" HeaderText="#"
                                UniqueName="column1">
                            </telerik:GridClientSelectColumn>
                            <telerik:GridTemplateColumn DataField="Name" FilterControlAltText="Filter TemplateColumn column"
                                HeaderText="Name" UniqueName="TemplateColumn">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lbtn" runat="server"></asp:LinkButton>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridBoundColumn DataField="Description" FilterControlAltText="Filter column column"
                                HeaderText="Description" UniqueName="column">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="UpdatedBy" FilterControlAltText="Filter column2 column"
                                HeaderText="Updated By" UniqueName="column2">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="UpdatedDate" FilterControlAltText="Filter column3 column"
                                HeaderText="Updated Date" UniqueName="column3">
                            </telerik:GridBoundColumn>
                        </Columns>
                        <EditFormSettings>
                            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                            </EditColumn>
                        </EditFormSettings>
                    </MasterTableView>
                    <FilterMenu EnableImageSprites="False">
                    </FilterMenu>
                </telerik:RadGrid>
            </div>
        </telerik:RadPageView>
        <telerik:RadPageView ID="RadPageView2" runat="server">
            <div class="ContentTabBody">
                <table>
                    <tr style="vertical-align: top;">
                        <td>
                            <asp:HiddenField ID="hfRoleID" runat="server" />
                            <table>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label1" runat="server" Text="Name :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadTextBox ID="rtbxName" runat="server" ClientEvents-OnValueChanged="rtbxName_OnvalueChanged">
                                        </telerik:RadTextBox>                                        
                                    </td>
                                </tr>
                                <tr style="vertical-align: top; height: 50px">
                                    <td>
                                        <asp:Label ID="Label2" runat="server" Text="Description :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadTextBox ID="rtbxDiscription" runat="server" TextMode="MultiLine" Height="49px">
                                        </telerik:RadTextBox>
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label4" runat="server" Text="Is Disabled :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbIsDisabled" runat="server" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td>
                            <table>
                                <tr>
                                    <td class="ContentHeading">
                                        <asp:Label ID="Label3" runat="server" Text="System Privileges :" SkinID="ContentHeading"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <telerik:RadTreeView ID="rtvPrivileges" runat="server" OnClientLoad="rtvPrivileges_OnClientLoad"
                                            CheckBoxes="True" TriStateCheckBoxes="True">
                                        </telerik:RadTreeView>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </telerik:RadPageView>
    </telerik:RadMultiPage>
    <script type="text/javascript">

        function rtbxName_OnvalueChanged(sender, args) {
            var rMgmt = initRoleMgmt();
            rMgmt.CheckForDuplicateRoleName(args);            
        }

        function rtbModuleActions_OnClientButtonClicked(sender, args) {
            processCommands(args.get_item().get_commandName());
            args.get_item().set_checked(false);
        }

        function rgRoles_OnGridCreated(sender, args) {
            var gridMasterTable = $find('<%=rgRoles.ClientID %>').get_masterTableView();
            gridMasterTable.set_dataSource(new Array());
            gridMasterTable.dataBind();
        }

        function processCommands(pCmdName) {
            switch (pCmdName) {
                case 'SAVE':
                    {
                        if (GLOBAL_CheckUserHasPrivilege('CREATE') || GLOBAL_CheckUserHasPrivilege('EDIT')) {
                            var rMgmt = initRoleMgmt();
                            rMgmt.PerformSaveOperation();
                        }
                        else {
                            N_NotifyMessage('Access Denied !!!', 'You don\'t have privileges to perform this operation. Please contact administrator ', 2);
                        }
                        break;
                    }

                case 'LIST':
                    {
                        if (GLOBAL_CheckUserHasPrivilege('SEARCH')) {
                            var rMgmt = initRoleMgmt();
                            rMgmt.PerformListOperation();
                        }
                        else {
                            N_NotifyMessage('Access Denied !!!', 'You don\'t have privileges to perform this operation. Please contact administrator ', 2);
                        }
                        break;
                    }

                case 'DELETE':
                    {
                        if (GLOBAL_CheckUserHasPrivilege('DELETE')) {
                            var rMgmt = initRoleMgmt();
                            rMgmt.PerformDeleteOperation();
                        }
                        else {
                            N_NotifyMessage('Access Denied !!!', 'You don\'t have privileges to perform this operation. Please contact administrator ', 2);
                        }
                        break;
                    }

                case 'NEW':
                    {
                        if (GLOBAL_CheckUserHasPrivilege('EDIT')) {
                            var rMgmt = initRoleMgmt();
                            rMgmt.PerformNewOperation();
                        }
                        else {
                            N_NotifyMessage('Access Denied !!!', 'You don\'t have privileges to perform this operation. Please contact administrator ', 2);
                        }
                        break;
                    }
            }
        }  

        function rgRoles_OnRowDataBound(sender, args) {

            var lblBtn = args.get_item().findElement("lbtn");
            if (lblBtn != null) {
                lblBtn.innerHTML = args.get_dataItem()['Name'];
                lblBtn.href = "javascript:loadRoleData('" + args.get_dataItem()['ID'] + "')";
            }
            else {
                var cell1 = args.get_item().get_cell('TemplateColumn');
                var ctrl = "<a href='javascript:loadRoleData({0})'>{1}</a>";
                cell1.innerHTML = String.format(ctrl, String.format("'{0}'", args.get_dataItem()['ID']), args.get_dataItem()['Name']);
            }
        }

        function loadRoleData(pRoleID) {
            var rMgmt = initRoleMgmt();
            rMgmt.LoadRoleData(pRoleID);
        }

        function rgRoles_OnCommand(sender, args) {
            switch (args.get_commandName()) {
                case 'PageSize':
                case 'Page':
                    {
                        var rMgmt = initRoleMgmt();
                        rMgmt.PerformListOperation();
                        break;
                    }
            }
            args.set_cancel(true);
        }

        function initRoleMgmt() {
            var roleMgmt = new RoleMgmt();
            roleMgmt.RoleTabsStrip = $find('<%=rtsRoleManagement.ClientID %>');
            roleMgmt.DescriptionBox = $find('<%=rtbxDiscription.ClientID %>');
            roleMgmt.DisableCheck = $get('<%=cbIsDisabled.ClientID %>');
            roleMgmt.NameBox = $find('<%=rtbxName.ClientID %>');
            roleMgmt.PrivilegeTree = $find('<%=rtvPrivileges.ClientID %>');
            roleMgmt.RoleID = $get('<%=hfRoleID.ClientID %>');
            roleMgmt.SearchGrid = $find('<%=rgRoles.ClientID %>');

            return roleMgmt;
        }

        function perfromNew() {

            var rMgmt = initRoleMgmt();
            rMgmt.PerformNewOperation();
        } 

        function rtvPrivileges_OnClientLoad(sender, args) {            
            var rMgmt = initRoleMgmt();
            rMgmt.LoadPrivileges();
        }        

    </script>
</asp:Content>
